/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.token.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opgea.token.service.DestinationService;
import com.opgea.token.service.TokenDetailsService;
import com.opgea.token.util.FacesUtil;
import com.opgea.token.web.dto.DestinationDTO;
import com.opgea.token.web.dto.TokenDetailsDTO;

/**
 * 
 * @author Ramesh
 */
@Controller
@Scope("request")
@ManagedBean(name = "destinationController")
@RequestScoped
public class DestinationController {

    @Autowired
    private DestinationService destinationService;
    @Autowired
    private TokenDetailsService tokenDetailsService;

    private DestinationDTO destinationDTO = new DestinationDTO();
    private List<DestinationDTO> destinationList = new ArrayList<DestinationDTO>();
    private List<TokenDetailsDTO> tokenList = new ArrayList<TokenDetailsDTO>();

    @PostConstruct
    public void init() {
	Long companyId = FacesUtil.getSessionUserDataObject().getCompanyId();
	destinationList = destinationService.findAllByCompany(companyId);
	if (destinationService.findAllByCompany(companyId) != null) {
	    destinationList = destinationService.findAllByCompany(companyId);
	}
	if (destinationList != null && destinationList.size() > 0) {
	    // destinationDTO = destinationList.get(0);
	}
    }

    public DestinationDTO getDestinationDTO() {
	return destinationDTO;
    }

    public void setDestinationDTO(DestinationDTO destinationDTO) {
	this.destinationDTO = destinationDTO;
    }

    public List<DestinationDTO> getDestinationList() {
	return destinationList;
    }

    public void setDestinationList(List<DestinationDTO> destinationList) {
	this.destinationList = destinationList;
    }

    public List<TokenDetailsDTO> getTokenList() {
	return tokenList;
    }

    public void setTokenList(List<TokenDetailsDTO> tokenList) {
	this.tokenList = tokenList;
    }

    public String save() {
	Long companyId = FacesUtil.getSessionUserDataObject().getCompanyId();
	destinationDTO.setCompanyId(companyId);
	destinationService.update(destinationDTO);
	if (destinationDTO.getId() > 0) {
	    FacesContext.getCurrentInstance().addMessage(
		    "messages",
		    new FacesMessage(FacesMessage.SEVERITY_INFO,
			    "Updated Successfully!", null));

	    return "create.xhtml";
	} else {
	    FacesContext.getCurrentInstance().addMessage(
		    "messages",
		    new FacesMessage(FacesMessage.SEVERITY_INFO,
			    "Created Successfully!", null));
	    return "new.xhtml";
	}
    }

    public String edit() {
	String destinationId = (String) FacesUtil
		.getRequestParameter("destinationId");
	destinationDTO.setId(new Long(destinationId));
	destinationDTO = destinationService.find(destinationDTO);
	return "create.xhtml";
    }
}
